<?php
/**
 * Zend Framework
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://framework.zend.com/license/new-bsd
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@zend.com so we can send you a copy immediately.
 *
 * @category   Zend
 * @package    Zend_Serializer
 * @subpackage Profiler
 * @copyright  Copyright (c) 2010 COPEO
 */

require_once 'Zend/Serializer.php';

/**
 * serialize data before sending it back.
 * 
 * @category    Myzen
 * @package    Myzen_Serializer
 * @subpackage Profiler
 * @copyright  Copyright (c) 2010 COPEO
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 */
class Myzen_Serializer
{
    /**
     * Contains the log object on which data should be logged against
     * @var int
     */
    protected $_logger = null; 
    
    /**
     * Contains the level on which the log should be written
     * @var int
     */
    protected $_level = 7;    
  
	// as HTMl is case insensentive, we need to convert the names of the serializer to lower case
  	static protected $_serializerConversion = array(
  		'serializedphp' => 	'PhpSerialize',
  		'json' 			=>	'Json',
  		'simplexml' 	=>	'SimpleXML',
  		'igbinary'		=> 	'Igbinary',
  		'wddx'			=>	'Wddx',
  		'amf0'			=>	'Amf0',
  		'amf3'			=>	'Amf3',					
  		'pythonpickle'	=>	'PythonPickle',
  		'phpcode'		=> 	'PhpCode'	
  	);
    /**
     * Constructor
     *
     * @param object $oLog OPTIONAL logger
     * @param int $level OPTIONAL level on which we need to log the data     
     * @return void
     */
    public function __construct($oLog = null,$level = 7)
    {
		if(isset($oLog) && !($oLog instanceof Zend_Log)) {
			require_once('Myzen/Serializer/Exception.php');
			throw new Myzen_Serializer_Exception('serializer: Log object provided is not an instance of Zend_Log');			
		}

		$this->_logger = $oLog;
		$this->_level = (int) $level;
		
		//We add our custom path for serializer
		require_once('Zend/Serializer.php');
		$loader = Zend_Serializer::getAdapterLoader();
		$loader->addPrefixPath('Myzen_Serializer_Adapter','Myzen/Serializer/Adapter');

    }

	
	public function serialize($data,$name = '',$options = array()) {

		//If the name is provided, we need to select the default serializer, else we can consider the user did the call before calling this function
		
		//if the app specificaly called
		if(isset($name)  && in_array($name,array_keys(self::$_serializerConversion))) {
			$this->_logger->log('changing serializer to '.$name,$this->_level);
			Zend_Serializer::setDefaultAdapter(self::$_serializerConversion[$name], $options);
		} else {
			// We check if there is not an output parameter that tells which serializer is expected
			$output = Zend_Controller_Front::getInstance()->getRequest()->getParam('output');

			if(isset($output)  && in_array($output,array_keys(self::$_serializerConversion))) {
				$this->_logger->log('Using output parameter, changing serializer to '.$output,$this->_level);
				Zend_Serializer::setDefaultAdapter(self::$_serializerConversion[$output], $options);
			} 
		}

		$string = Zend_Serializer::serialize($data, $options);
		if(isset($this->_logger)) $this->_logger->log('message: '.$string,$this->_level);
		
		return $string;
			
	}
	
}
